import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-07-16
 * Time: 23:20
 */

public class Solution {
    public int[] arr;

    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        int len = rooms.size();
        this.arr = new int[len];
        dfs(rooms, 0);
        return isVisitAll();
    }

    private boolean isVisitAll() {
        int len = arr.length;
        for (int i = 0; i < len; i++) {
            if (this.arr[i] == 0) {
                return false;
            }
        }
        return true;
    }

    private void dfs(List<List<Integer>> rooms, int index) {
        this.arr[index] = 1;
        List<Integer> list = rooms.get(index);
        for (Integer i: list) {
            // 房间还没有被访问过
            if (this.arr[i] == 0) {
                dfs(rooms, i);
            }
        }
    }
}
